//
//  EyeTestViewModel.swift
//  App_Vision
//
//  Created by Flutter on 2021/7/28.
//

import Foundation
import UIKit
import UICore
import RxCocoa
import RxSwift
import DeviceKit
import App_Common


class EyeTestViewModel: ViewModel {
    
    
    var disposeBag: DisposeBag?
    
    let eyeTest: BehaviorRelay<EyeTestModel> = {
        let defaultDistance = VisionCenter.visionInfo.defaultDistance ?? VisionTable.recommendDefaultDistance
        let defaultVisionIndex = VisionCenter.visionInfo.defaultVisionIndex ?? VisionTable.recommendDefaultVisionIndex
        let defaultDirection = EView.Direction(rawValue: Int(arc4random_uniform(4))) ?? .Unknown
        let defaultGlass = VisionCenter.visionInfo.defaultGlass ?? false
        let defaultEye = Eye.left
        return BehaviorRelay(value: EyeTestModel(distance: defaultDistance, visionIndex: defaultVisionIndex, direction: defaultDirection, glass: defaultGlass, eye: defaultEye,wrong: 0,right: 0))
    }()

    let message = BehaviorRelay(value: "")
    
    /// 用于得出用户视力结果的参数
    var isLastVisible: Bool?
    
    /// 当前视力正确的次数， 正确 3次 就加视力检查
    var rightCount: UInt = 0
    
    /// 当前视力错误的次数，错误 3次 就减视力检查
    var wrongCount: UInt = 0
    
    // 该次的检查结果
    let next: BehaviorRelay<EyeTestResult> = BehaviorRelay(value: EyeTestResult())
}


extension EyeTestViewModel {
    struct Input {
        let swipe: Observable<EView.Direction>
        let next: Observable<Eye>
//        let distance: Observable<Float>
//        let glass: Observable<Bool>
//        let deltaVisionIndex: Observable<Int>
//        let visionIndex: Observable<Int>
//        let nextEye: Observable<Eye>
    }

    struct Output {
        let eyeTest: BehaviorRelay<EyeTestModel>
        let next: BehaviorRelay<EyeTestResult>
//        let direction: BehaviorRelay<EView.Direction>
//        let visionIndex: BehaviorRelay<Int>
//        let glass: BehaviorRelay<Bool>
//        let distance: BehaviorRelay<Float>
//        let eye: BehaviorRelay<Eye>
//        let message: BehaviorRelay<String>
//        let result: BehaviorRelay<EyeTestResult>
    }

    func transform(input: Input) -> Output {
        bindSwipe(input.swipe)
        bindNextEye(input.next)
//        bindVisionOptions(input.distance, input.visionIndex, input.glass, input.deltaVisionIndex)
        return Output(
//            direction: eDirection,
//            visionIndex: visionIndex,
//            glass: glass,
//            distance: distance,
//            eye: eye,
//            message: message,
//            result: result
            eyeTest: eyeTest,
            next: next
        )
    }
}

extension EyeTestViewModel {

    
    func bindSwipe(_ swipe: Observable<EView.Direction>) {
        
        guard let disposeBag = self.disposeBag else { return }
        swipe.subscribe {[weak self] event in
            guard let self = self else { return }
            guard let userDirection = event.element else { return }

            var eyeTest = self.eyeTest.value
            let currentDirection = eyeTest.direction
            if(userDirection == currentDirection) {
                eyeTest.right+=1
//                self.rightCount+=1
            }else {
                eyeTest.wrong+=1
//                self.wrongCount+=1
            }
            
            if eyeTest.right > 2 {
                // 添加每次详情
                self.next.value.addDetail(eye: eyeTest.eye, visionIndex: eyeTest.visionIndex, wrong: eyeTest.wrong, right: eyeTest.right)
                
                if self.isLastVisible == false {
                    self.nextResult(visionIndex: eyeTest.visionIndex, eye: eyeTest.eye)
                    return
                }
                eyeTest.visionIndex+=1
                eyeTest.wrong = 0
                eyeTest.right = 0
                self.isLastVisible = true
                if eyeTest.visionIndex > VisionTable.maxVisionIndex {
                    eyeTest.visionIndex = VisionTable.maxVisionIndex
                    self.nextResult(visionIndex: eyeTest.visionIndex, eye: eyeTest.eye)
                    return
                }
            }else if eyeTest.wrong > 2 {
                // 添加每次详情
                self.next.value.addDetail(eye: eyeTest.eye, visionIndex: eyeTest.visionIndex, wrong: eyeTest.wrong, right: eyeTest.right)
                
                if self.isLastVisible == true {
                    self.nextResult(visionIndex: eyeTest.visionIndex, eye: eyeTest.eye)
                    return
                }
                eyeTest.visionIndex-=1
                eyeTest.wrong = 0
                eyeTest.right = 0
                self.isLastVisible = false
                if eyeTest.visionIndex < VisionTable.minVisionIndex {
                    eyeTest.visionIndex = VisionTable.minVisionIndex
                    self.nextResult(visionIndex: eyeTest.visionIndex, eye: eyeTest.eye)
                    return
                }
            }
        
            let direction = EView.Direction(rawValue: Int(arc4random_uniform(4))) ?? .Unknown
            eyeTest.direction = direction
            
            self.eyeTest.accept(eyeTest)
            
        }.disposed(by: disposeBag)
    }
    
    func bindNextEye(_ nextEye: Observable<Eye>) {
        guard let disposeBag = self.disposeBag else { return }
        nextEye.subscribe {[weak self] event in
            guard let self = self else { return }
            guard let eye = event.element else { return }
            self.isLastVisible = nil
            var eyeTest = self.eyeTest.value
            eyeTest.right = 0
            eyeTest.wrong = 0
        
            eyeTest.visionIndex-=1
            if eyeTest.visionIndex < VisionTable.minVisionIndex {
                eyeTest.visionIndex = VisionTable.minVisionIndex
            }
            eyeTest.eye = eye
            self.eyeTest.accept(eyeTest)
            
        }.disposed(by: disposeBag)
    }
    
    func nextResult(visionIndex: Int, eye: Eye){
        let result = next.value
        if eye == .left {
            result.leftVisionIndex = visionIndex
        }else {
            result.rightVisionIndex = visionIndex
        }
        next.accept(result)
    }
    
    


    
}
